<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <title>搜索</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/main.css">
    <style>
        .pt {
            margin-top: 0.1rem !important;
        }
        html,
        body {
            background-color: #FFFFFF;
        }

        .news-content{
            margin-top: .88rem;
        }
    </style>
</head>

<body>
    <div class="app" v-cloak>
        <div class="header-common">
            <div class="flex-x-s header">
                <span class="iconfont goback" @click="back">&#xe66f;</span>
                <div class="search flex-x-s">
                    <span class="iconfont">&#xe71e;</span>
                    <input id="inputId" type="search" maxlength="20" placeholder="请搜索关键词" v-model="keyword"
                        @search="search">
                </div>
                <div class="search-btn" @click="search">搜索</div>
            </div>
        </div>
        <!-- <div class="pt">
            <div class="nosearch-bg" v-if="!hasHistory && !isSelected">
                <img src="https://zzzzkj.oss-cn-shanghai.aliyuncs.com/static/tiexie/app/image/image/search/nosearch-bg.png" alt="" class="nosearch-img">
                <div class="tips-text">暂无搜索历史</div>
            </div>
            <div class="search-history" v-show="hasHistory&&!isSelected">
                <div class="title">
                    <div class="title-text">
                        历史搜索
                    </div>
                    <div :class="['title-more', 'iconfont', isExpand ? 'title-expand' : '']" @click="expandSeachedList">
                        &#xe609;</div>
                </div>
                <div class="search-lable">
                    <span v-for="(item, index) in visitSearchedList" :key="index"
                        @click="selectHistory(item)">{{filterText(item)}}</span>
                </div>
            </div>
        </div> -->

        <div v-if="listNews.length>0" class="news-content">
            <!-- 资讯 -->
            <div class="news">
                <div class="news-item" v-for="(item,index) in listNews" :key="index" @click="goDetail(item)">
                    <div class="left">
                        <div class="new-title">{{item.title}}</div>
                        <div class="time">{{handleTimeYMD2(item.createTime)}}</div>
                    </div>
                    <div class="right">
                        <img v-lazy="item.coverPicture?item.coverPicture:'https://zzzzkj.oss-cn-shanghai.aliyuncs.com/static/tiexie/app/image/image/default/loading3.png'" alt=""
                            width="100" height="80" style="border-radius: 0.04rem;">
                    </div>
                </div>
            </div>
            <div class="no-more" v-if="!hasNext">没有更多了</div>
        </div>
        <div v-else class="collect-empty">
            <img src="https://zzzzkj.oss-cn-shanghai.aliyuncs.com/static/tiexie/app/image/image/empty/empty.png" alt="">
            <div>暂无内容～</div>
        </div>

    </div>
    <script src="../../script/api.js"></script>
    <script src="../../script/flexible.js"></script>
    <script src="../../script/fastclick.js"></script>
    <script src="../../script/vue.min.js"></script>
    <script src="../../script/iconfont.js"></script>
    <script src="../../script/restful.js"></script>
    <script src="../../script/common.js"></script>
    <script src="../../script/businessCommon.js"></script>
    <script src="../../script/vue-lazyload.js"></script>
    <script>
        apiready = function () {
            imgLazyLoad();
            vm = new Vue({
                el: '.app',
                data: {
                    title: api.pageParam.title || '',
                    titlePlus: '',
                    showProductList: [],
                    isExpand: false,
                    historyList: [],

                    keyword: '',
                    isSelected: false,

                    objectParam: {
                        pageNum: 1,
                        pageSize: 10,
                        channerId: null,
                        title: null
                    },
                    hasNext: true,
                    listNews: [],
                },
                created() {
                    console.log('参数:', JSON.stringify(api.pageParam));
                    this.historyList = ($api.getStorage("searchedList") && JSON.parse($api
                        .getStorage("searchedList"))) || []
                },
                mounted() {
                    this.initData()
                    this.autoFocus()

                    setBDPageStart("资讯搜索")
                },
                computed: {
                    hasHistory() {
                        return this.historyList.length !== 0;
                    },
                    visitSearchedList() {
                        if (this.isExpand) {
                            return this.historyList;
                        }
                        return this.historyList.slice(0, 8);
                    }
                },
                methods: {
                    getData() {
                        let vm = this
                        index.news("getListNews")(vm.objectParam, (ret) => {
                            vm.listNews = ret.data
                            if (vm.objectParam.pageNum == 1) {
                                vm.listNews = ret.data.list
                            } else {
                                vm.listNews = vm.listNews.concat(ret.data.list)
                            }
                            vm.hasNext = ret.data.hasNext
                        })
                    },
                    // 跳转到详情页
                    goDetail(item) {
                        api.openWin({
                            name: 'industry_news_detail_win',
                            url: 'industry_news_detail_win.html',
                            bounces: false,
                            pageParam: {
                                id: item.id,
                            }
                        });
                    },
                    initData() {
                        if (!api.pageParam) return false;
                        this.titlePlus = api.pageParam.titlePlus
                    },
                    // 自动聚焦
                    autoFocus() {
                        if (api.systemType == 'android') {
                            setTimeout(() => {
                                var softInput = api.require('softInputMgr');
                                softInput.toggleKeyboard();
                            }, 100)
                        }
                        document.querySelector('#inputId').focus()
                    },
                    back() {
                        setBDPageEnd("资讯搜索")
                        api.closeWin();
                    },
                    selectHistory(item) {
                        this.keyword = item
                        this.search();
                    },
                    saveSearchedKeyword() {
                        this.historyList = this.historyList.filter(item => item !== this.keyword);
                        if (this.keyword) {
                            this.historyList.unshift(this.keyword);
                        }
                        if (this.historyList.length > 15) {
                            this.historyList.splice(this.historyList.length - 1, 1)
                        }
                        $api.setStorage("searchedList", JSON.stringify(this.historyList))
                    },
                    search() {
                        if (this.keyword) {
                            if (this.keyword.length >= 10) {
                                toastBottom('长度需要在10字以内');
                                return false
                            }
                        }
                        this.saveSearchedKeyword();
                        vm.isSelected = true;

                        this.objectParam.title = this.keyword
                        this.getData()
                        // api.sendEvent({
                        //     name: 'searchNews',
                        //     extra: {
                        //         keyword: this.keyword
                        //     }
                        // })

                        // this.back()
                    },
                    expandSeachedList() {
                        var vm = this
                        api.confirm({
                            title: '是否清空历史搜索',
                            buttons: ['确定', '取消']
                        }, function (ret, err) {
                            if (ret.buttonIndex == 1) {
                                vm.isExpand = !vm.isExpand;
                                vm.historyList = [];
                                $api.setStorage(vm.title + "searchedList", JSON.stringify(vm.historyList))
                            }
                        });
                    },
                },
            })
            api.addEventListener({
                name: 'keyback'
            }, function (ret, err) {
                setBDPageEnd("资讯搜索")
                api.closeWin();
            });

            // 上拉加载
            api.addEventListener({
                name: 'scrolltobottom',
                extra: {
                    threshold: -10
                }
            }, function (ret, err) {
                console.log('加载');
                if (vm.hasNext) {
                    vm.objectParam.pageNum++
                    vm.getData();
                }
            });
        }
    </script>
</body>

</html>